# coding=utf8

from django.contrib.auth.models import User
from django import forms
from models import *
import re
from choices import *

pattern = re.compile(r'\w+')
class_choices = Class.objects.values_list('id', 'name')

class ActiveForm(forms.Form):
    name = forms.CharField(max_length=50)
    desc = forms.CharField(required=False)
    
class AtomForm(forms.Form):
    active_type = forms.IntegerField(min_value=0)
    label = forms.CharField(max_length=50, required=False)
    atom = forms.ChoiceField(choices=atom_choices)
    
    def clean_active_type(self):
        try:
            active_type = ActiveType.objects.get(pk=int(self.cleaned_data['active_type']))
        except ActiveType.DoesNotExist:
            raise forms.ValidationError("活动类型不存在！")
        
        return active_type

class ActiveTypeForm(forms.Form):
    label = forms.CharField(max_length=50)
    desc = forms.CharField(required=False)

class DataTableForm(forms.Form):
    sSearch = forms.CharField(max_length=50, required=False)
    sEcho = forms.CharField(max_length=100, required=False)
    iColumns = forms.IntegerField(min_value=0)
    iDisplayLength = forms.IntegerField(min_value=0)
    iDisplayStart = forms.IntegerField(min_value=0)
    sColumns = forms.IntegerField(required=False, min_value=0)
    bRegex = forms.CharField()
    iSortingCols = forms.IntegerField(min_value=0)
    
    def clean_bRegex(self):
        bRegex = self.cleaned_data['bRegex']
        if bRegex == 'true' or bRegex == 'false':
            return bRegex
        else:
            raise forms.ValidationError("课程已存在！")

class TestForm(forms.Form):
    active_id = forms.IntegerField()
    atom_id = forms.IntegerField()
    oj_plat = forms.ChoiceField(choices=oj_choices)
    contest_id = forms.CharField(max_length=20)
    
    def clean_active_id(self):
        try:
            active = Active.objects.get(pk=self.cleaned_data['active_id'])
        except Active.DoesNotExist:
            raise forms.ValidationError("活动不存在！")
        return active

    def clean_atom_id(self):
        try:
            atom = ActiveTypeAtom.objects.get(pk=self.cleaned_data['atom_id'])
        except ActiveTypeAtom.DoesNotExist:
            raise forms.ValidationError("原子操作不存在！")
        return atom

class ClassForm(forms.Form):
    code = forms.CharField(max_length=20, required=False)
    name = forms.CharField(max_length=50)
    desc = forms.CharField(required=False)

    
class CourseForm(forms.Form):
    code = forms.CharField(max_length=20)
    name = forms.CharField(max_length=50)
    syllabus = forms.CharField()
    experiment = forms.CharField(required=False)
    
    def clean_code(self):
        code = self.cleaned_data['code']
        try:
            Course.objects.get(code=code)
            raise forms.ValidationError("课程已存在！")
        except Course.DoesNotExist:
            return code
        
class LessonForm(forms.Form):
    course_id = forms.IntegerField()
    code = forms.CharField(max_length=20)
    clses = forms.MultipleChoiceField(choices=class_choices, required=False)
    desc = forms.CharField(required=False)
    
    def clean_course_id(self):
        course_id = self.cleaned_data['course_id']
        try:
            Course.objects.get(pk=course_id)
            return course_id
        except Lesson.DoesNotExist:
            raise forms.ValidationError("课程不存在！")
    
    def clean_code(self):
        code = self.cleaned_data['code']
        try:
            Lesson.objects.get(code=code)
            raise forms.ValidationError("单元已存在！")
        except Lesson.DoesNotExist:
            return code
        
    def clean_clses(self):
        clses = self.cleaned_data['clses']
        for cls in clses:
            try:
                Class.objects.get(pk=int(cls))
            except Class.DoesNotExist:
                raise forms.ValidationError("班级不存在！")
        return clses
    
class StuImportFileForm(forms.Form):
    stu_file = forms.FileField()
    

class StuImportTextForm(forms.Form):
    stu_list = forms.CharField()
    